package com.example.testrssparser;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;
import android.util.Xml;
import android.widget.Toast;

public class XmlPullFeedParser extends BaseFeedParser  {
	
	static SimpleDateFormat FORMATTER = 
	        new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
	
	@Override
	public List<Message> parse(InputStream is) throws CustomParseException {
		List<Message> messages = null;
		Message currentMessage = null;
		boolean done = false;
		//String name = null;
		try {
			XmlPullParser parser = Xml.newPullParser();
			parser.setInput(is, null);
			int eventType = parser.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT && !done) {
				String name = null;
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					messages = new ArrayList<Message>();
					break;
				case XmlPullParser.START_TAG:
					name = parser.getName();
					if (name.equalsIgnoreCase(ITEM)) {
						currentMessage = new Message();
					} else if (currentMessage != null) {
						if (name.equalsIgnoreCase(LINK)) {
							URL link = new URL(parser.nextText());
							currentMessage.setLink(link);
						} else if (name.equalsIgnoreCase(DESCRIPTION)) {
							currentMessage.setDescription(parser.nextText());
						} else if (name.equalsIgnoreCase(PUB_DATE)) {
							currentMessage.setDate(parseDate(parser.nextText()));
						} else if (name.equalsIgnoreCase(TITLE)) {
							currentMessage.setTitle(parser.nextText());
						} else if (name.equalsIgnoreCase(AUTOR)) {
							currentMessage.setAutor(parser.nextText());
						}
					}
					break;
				/*	
				case XmlPullParser.TEXT:
					if (currentMessage != null) {
						if (name.equalsIgnoreCase(LINK)) {
							currentMessage.setLink(parser.getText());
						} else if (name.equalsIgnoreCase(DESCRIPTION)) {
							currentMessage.setDescription(parser.getText());
						} else if (name.equalsIgnoreCase(PUB_DATE)) {
							currentMessage.setDate(parseDate(parser.getText()));
						} else if (name.equalsIgnoreCase(TITLE)) {
							currentMessage.setTitle(parser.getText());
						} else if (name.equalsIgnoreCase(AUTOR)) {
							currentMessage.setAutor(parser.getText());
						}
					}
					break;
				*/	
				case XmlPullParser.END_TAG:
					name = parser.getName();
					if (name.equalsIgnoreCase(ITEM) && currentMessage != null) {
						messages.add(currentMessage);
						currentMessage = null;
					} else if (name.equalsIgnoreCase(CHANNEL)) {
						done = true;
					}
					break;
				}
				eventType = parser.next();
			}
		} catch (XmlPullParserException e) {
			throw new CustomParseException("XmlPullFeedParser::parse XmlPullParserException", e);
		} catch (MalformedURLException e) {
			throw new CustomParseException("XmlPullFeedParser::parse MalformedURLException", e);
		} catch (ParseException e) {
			throw new CustomParseException("XmlPullFeedParser::parse ParseException", e);
		} catch (IOException e) {
			throw new CustomParseException("XmlPullFeedParser::parse IOException", e);
		} 
		return messages;
	}
	
	private Date parseDate(String date) throws ParseException {
		while (!date.endsWith("00")){
            date += "0";
        }
        return FORMATTER.parse(date.trim());
    }
}